<?php

namespace app\index\controller;

use think\Db;
use think\File;

class User extends Common {

    protected $table = 'chat_userinfo';
    protected $like_table = 'chat_userlike';

    // 个人资料修改
    //用户中心

    public function updateUserInfo() {
        $insert = [];
        $insert['user_name'] = input('param.user_name');
        $insert['user_logo'] = input('param.user_logo');
        $insert['user_sex'] = input('param.user_sex');
        $insert['age'] = input('param.age');
        $insert['user_province'] = input('param.user_province');
        $insert['user_address'] = input('param.user_address');

        $insert['constellation'] = input('param.constellation');
        $insert['signature'] = input('param.signature');

        $user_id = input('param.user_id');
        $insert['ctime'] = time();

        if (Db::name($this->table)->where(['user_id' => $user_id])->update($insert)) {
            return retData($insert, '1000', '修改成功');
        } else {
            return retData([], '1001', '修改失败');
        }
    }

    //获取我的关注列表
    public function getLikeList() {
        $user_uid = input('param.user_uid');
        $app_flag = input('param.app_flag');

        $data = Db::name($this->like_table)->where(['user_uid' => $user_uid, 'is_follow' => 0, 'app_flag' => $app_flag])->select('user_uid_beinglike');
        $list = [];
        if (!empty($data)) {
            $arr = array_column($data, 'user_uid_beinglike');
            $list = $this->getUserById($arr, 1);
        }

        return retData($list, '1000', 'ok');
    }

    //添加关注 主播

    public function addLike() {
        $user_uid = input('param.user_uid');
        $app_flag = input('param.app_flag');
        $user_uid_beinglike = input('param.user_uid_beinglike');

        $insert = [
            'user_uid' => $user_uid,
            'app_flag' => $app_flag,
            'user_uid_beinglike' => $user_uid_beinglike,
            'ctime' => time()
        ];

        if (Db::name($this->like_table)->insert($insert)) {
            return retData($insert, '1000', '成功');
        } else {
            return retData([], '1001', '失败');
        }
    }

    //取消关注 主播

    public function removeLike() {
        $user_uid = input('param.user_uid');
        $app_flag = input('param.app_flag');
        $user_uid_beinglike = input('param.user_uid_beinglike');

        $map = [
            'app_flag' => $app_flag,
            'user_uid_beinglike' => $user_uid_beinglike,
        ];

        if (Db::name($this->like_table)->where($map)->update(['is_follow' => 1])) {
            return retData([], '1000', '成功');
        } else {
            return retData([], '1001', '失败');
        }
    }

    /*     * ************************* 主播认证************************************* */

    protected $img_table = 'chat_userimgsidentity'; // 认证相册表
    protected $video_table = 'chat_uservideosidentity'; // 认证视频表
    protected $identity_table = 'chat_useridentity'; // 主播认证表
    protected $tag_table = 'chat_tag'; // 主播自我评价标签

    // 主播认证资料

    public function updateIdentityUserInfo() {
        $insert = [];
        $insert['user_name'] = input('param.user_name');
        $insert['user_logo'] = input('param.user_logo'); //主图
        $insert['user_height'] = input('param.user_height'); //user_weight 
        $insert['user_weight'] = input('param.user_weight'); //     
        $insert['age'] = input('param.age');
        $insert['user_province'] = input('param.user_province', '');
        $insert['user_address'] = input('param.user_address');

        $insert['constellation'] = input('param.constellation');
        $insert['signature'] = input('param.signature');

        $insert['user_tag'] = input('param.user_tag');


        $user_id = input('param.user_id');
        $user_uid = input('param.user_uid');
        $app_flag = input('param.app_flag');
        $insert['ctime'] = time();

        // 修改主播资料
        Db::startTrans();
        try {
            //基础资料修改
            Db::table($this->table)->where(['user_id' => $user_id])->update($insert);
            //认证照片
            $photo = explode(',', input('param.photo')); //多张照片，格式 ，
            $identity_photo = [];
            foreach ($photo as $v) {
                $identity_photo = [
                    'user_uid' => $user_uid,
                    'img' => $v,
                    'app_flag' => $app_flag
                ];
            }
            Db::table($this->img_table)->insertAll($identity_photo);

            //认证视频上传
            $video_arr = [
                'video' => input('param.video'),
                'video_time' => input('param.video_time'),
                'ctime' => time(),
                'app_flag' => $app_flag
            ];
            Db::table($this->video_table)->insert($video_arr);
            // 提交事务
            Db::commit();
            return retData($insert, '1000', '成功');
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return retData([], '1001', '失败');
        }
    }

    // 上传图片 支持多个
    public function uploadImg() {
        $data = MediaUpload('user', 0);
        return $data;
    }

    // 上传视频 
    public function uploadVideo() {
        $data = MediaUpload('user', 1);
        return $data;
    }

    // 获取主播标签, 不区分app_flag
    public function getTagList() {
        $list = [];
        $list = Db::name($this->tag_table)->select();
        return retData($list, '1000', 'ok');
    }

    //获取认证状态
    //state  1未审核2待审核3通过4驳回
    public function getIdentityState() {
        $data = Db::table($this->identity_table)->field('state')->where([
                    'user_uid' => $this->user_uid,
                    'app_flag' => $this->app_flag,
                ])->find();

        if ($data) {
            return retData(['state' => $data['state']], '1000', 'ok');
        } else {
            return retData([], '1001', '没有申请认证');
        }
    }

   
}
